﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;

namespace SimpleText.Framework
{

	public class LanguageVersioningModel
	{
		public string ProjectName { get; set; }
		public string Description { get; set; }
		public string MasterLanguageShortName { get; set; }
		public List<Language> Languages { get; set; }
		public Dictionary<string, TextDefinition> TextDefinitions { get; set; }
		public string LoadedFromXmlFilePath { get; set; }

		public LanguageVersioningModel(string xml)
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(xml);

			InitLanguageVersioningModel(xmlDoc);
		}
		public LanguageVersioningModel(XmlDocument xmlDoc)
		{
			InitLanguageVersioningModel(xmlDoc);
		}

		protected void InitLanguageVersioningModel(XmlDocument xmlDoc)
		{
			Languages = new List<Language>();
			TextDefinitions = new Dictionary<string, TextDefinition>();

			ProjectName = xmlDoc.SelectSingleNode("/languageversioning").Attributes["projectName"].Value;
			if (xmlDoc.SelectSingleNode("/languageversioning/description") != null)
			{
				Description = xmlDoc.SelectSingleNode("/languageversioning/description").InnerText;
			}
			if (xmlDoc.SelectSingleNode("/languageversioning").Attributes["masterLanguageShortName"] != null)
			{
				MasterLanguageShortName = xmlDoc.SelectSingleNode("/languageversioning").Attributes["masterLanguageShortName"].Value;
			}

			//languages
			XmlNodeList languageNodes = xmlDoc.SelectNodes("//language");
			foreach (XmlNode languageNode in languageNodes)
			{
				Language newLan = new Language();
				newLan.Name = languageNode.Attributes["name"].Value;
				newLan.ShortName = languageNode.Attributes["shortName"].Value;
				newLan.CultureName = languageNode.Attributes["cultureName"].Value;

				Languages.Add(newLan);
			}

			//textdefinitions
			XmlNodeList textDefNodes = xmlDoc.SelectNodes("//textDefinitions/textDefinition");
			foreach (XmlNode textDefNode in textDefNodes)
			{
				TextDefinition textDef = new TextDefinition();
				textDef.InitWithXml(textDefNode);

				TextDefinitions.Add(textDef.Name, textDef);
			}

		}

		public string GetAsXml()
		{
			string xml = "";

			xml +=
			"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\r\n" +
			"<languageversioning" + "\r\n" +
			"	projectName=\"" + ProjectName + "\"" + "\r\n" +
			"	masterLanguageShortName=\"" + MasterLanguageShortName + "\"" + "\r\n" +
			">" + "\r\n" +
			"";

			string indent = "\t";
			if (!String.IsNullOrEmpty(Description))
			{
				xml += indent + "<description><![CDATA[" + Description + "]]></description>" + "\r\n";
			}

			xml += indent + "<languages>" + "\r\n";
			foreach (Language language in Languages)
			{
				xml += language.GetAsXml(indent + "\t") + "\r\n";
			}
			xml += indent + "</languages>" + "\r\n";

			xml += indent + "<textDefinitions>" + "\r\n";
			foreach (TextDefinition textDef in TextDefinitions.Values)
			{
				xml += textDef.GetAsXml(indent + "\t") + "\r\n";
			}
			xml += indent + "</textDefinitions>" + "\r\n";

			xml += "</languageversioning>" + "\r\n";

			return xml;
		}

	}
}